import Vue from "vue"
import VueRouter, { RouteConfig } from "vue-router"
import Layout from "@/layout/index.vue"
import store from "@/store"
import advertSpace from "@/views/advert-space/index.vue"
Vue.use(VueRouter)
const routes: Array<RouteConfig> = [
  {
    path: "/login",
    name: "login",
    component: () =>
      import(/* webpackChunkName:'login' */ "@/views/login/index.vue")
  },

  {
    path: "/",
    component: Layout,
    meta: {
      // 默认就是一个空对象
      requiresAuth: true // 存放自定义数据
    },
    children: [
      {
        path: "/advert",
        name: "advert",
        component: () =>
          import(/* webpackChunkName:'advert' */ "@/views/advert/index.vue")
      },
      {
        path: "/advert-space",
        name: "advert-space",
        component: advertSpace
      },
      {
        path: "/course",
        name: "course",
        component: () =>
          import(/* webpackChunkName:'course' */ "@/views/course/index.vue")
      },
      {
        path: "/",
        name: "home",
        component: () =>
          import(/* webpackChunkName:'home' */ "@/views/home/index.vue")
      },
      {
        path: "/menu",
        name: "menu",
        component: () =>
          import(/* webpackChunkName:'menu' */ "@/views/menu/index.vue")
      },
      {
        path: "/menu/create",
        name: "menu-create",
        component: () =>
          import(/* webpackChunkName:'menu-creat' */ "@/views/menu/create.vue")
      },
      {
        path: "/menu/:id/edit",
        name: "menu-edit",
        component: () =>
          import(/* webpackChunkName:'edit-menu' */ "@/views/menu/edit.vue")
      },
      {
        path: "/resource",
        name: "resource",
        component: () =>
          import(/* webpackChunkName:'resource' */ "@/views/resource/index.vue")
      },
      {
        path: "/role",
        name: "role",
        component: () =>
          import(/* webpackChunkName:'role' */ "@/views/role/index.vue")
      },
      {
        path: "/user",
        name: "user",
        component: () =>
          import(/* webpackChunkName:'user' */ "@/views/user/index.vue")
      },
      {
        path: "/resource/Classification",
        name: "resource-Classification",
        component: () =>
          import(
            /* webpackChunkName:'user' */ "@/views/resource/resourceClassification/resourceClassification.vue"
          )
      },
      {
        path: "/role/:roleId/alloc-menu",
        name: "alloc-menu",
        component: () =>
          import(/* webpackChunkName:'user' */ "@/views/role/alloc-menu.vue"),
        props: true // 将路由路径参数映射到组件的props中
      },
      {
        path: "/role/:roleId/alloc-resource",
        name: "alloc-resource",
        component: () =>
          import(/* webpackChunkName:'user' */ "@/views/role/alloc-resource.vue"),
        props: true // 将路由路径参数映射到组件的props中
      },
      {
        path: "/course/create",
        name: "course-create",
        component: () =>
          import(
            /* webpackChunkName:'course-edit' */ "@/views/course/Create.vue"
          )
      },
      {
        path: "/course/:courseId/edit",
        name: "course-edit",
        component: () =>
          import(/* webpackChunkName:'course-edit' */ "@/views/course/Edit.vue"),
        props: true
      },
      {
        path: "/course/:courseId/section",
        name: "course-section",
        component: () =>
          import(
            /* webpackChunkName:'course-section' */ "@/views/course/section.vue"
          ),
        props: true
      },
      {
        path: "/course/:courseId/video",
        name: "course-video",
        component: () =>
          import(
            /* webpackChunkName:'course-video' */ "@/views/course/video.vue"
          ),
        props: true
      }
    ]
  },
  {
    path: "*",
    name: "404",
    component: () =>
      import(/* webpackChunkName:'404' */ "@/views/error-page/404.vue")
  }
]
const router = new VueRouter({
  routes
})
// 全局前置守卫:任何页面的访问都要进过这个
// to:要去哪里的路由信息
// from:从那里来的路由信息
// next同行的标志 也就是允许放行

router.beforeEach((to, from, next) => {
  // to.matched 是一个数组（匹配到是路由记录）
  if (to.matched.some((record) => record.meta.requiresAuth)) {
    if (!store.state.user) {
      // 跳转到登录页面
      next({
        name: "login",
        query: {
          // 通过 url 传递查询字符串参数
          redirect: to.fullPath // 把登录成功需要返回的页面告诉登录页面
        }
      })
    } else {
      next() // 允许通过
    }
  } else {
    next() // 允许通过
  }
})
export default router
